//
//  OTNavBar.m
//  OT_OC
//
//  Created by yunmian on 2019/7/16.
//  Copyright © 2019 James. All rights reserved.
//

#import "OTNavBar.h"

static const NSInteger kNavBarTitleLabelWidth      = 30;
static const NSInteger kNavBarTitleLabelHeight     = 150;

@implementation OTNavBar

+ (instancetype)createNavBar
{
    OTNavBar *navBar = [[self alloc] initWithFrame:CGRectMake(0, 0, kScreenWidth, k_Height_NavBar)];
    return navBar;
}

- (instancetype)initWithFrame:(CGRect)frame
{
    self = [super initWithFrame:frame];
    if (self) {
        [self initUI];
    }
    return self;
}

- (void)initUI
{
    
    self.backgroundColor = [UIColor clearColor];
    
    backgroundView = [[UIView alloc] initWithFrame:self.bounds];
    backgroundView.backgroundColor = UIColor.whiteColor;
    [self addSubview:backgroundView];
    
    containerView = [[UIView alloc] init];
    containerView.backgroundColor = UIColor.whiteColor;
    [self addSubview:containerView];
    
    titleLabel = [[UILabel alloc] init];
    titleLabel.textAlignment = NSTextAlignmentCenter;
    [containerView addSubview:titleLabel];
    
    backButton = [UIButton buttonWithType:UIButtonTypeCustom];
    backButton.hidden = YES;
    [backButton setImage:[UIImage imageNamed:@"back"] forState:UIControlStateNormal];
    [backButton addTarget:self action:@selector(onClickBackButton) forControlEvents:UIControlEventTouchUpInside];
    [backButton setEnlargeEdgeWithTop:10 right:20 bottom:10 left:10];
    [containerView addSubview:backButton];
    
    rightButton = [UIButton buttonWithType:UIButtonTypeCustom];
    rightButton.hidden = YES;
    [rightButton setTitleColor:UIColor.blackColor forState:UIControlStateNormal];
    [rightButton addTarget:self action:@selector(onClickRightButton) forControlEvents:UIControlEventTouchUpInside];
    [rightButton setEnlargeEdgeWithTop:20 right:20 bottom:20 left:30];
    [containerView addSubview:rightButton];
}

- (void)layoutSubviews
{
    [super layoutSubviews];
    
    [containerView mas_remakeConstraints:^(MASConstraintMaker *make) {
        make.left.right.equalTo(self);
        make.top.offset(k_Height_StatusBar);
        make.height.offset(k_Height_NavContentBar);
    }];
    
    [titleLabel mas_remakeConstraints:^(MASConstraintMaker *make) {
        make.center.equalTo(self->containerView);
        make.height.offset(kNavBarTitleLabelWidth);
        make.width.offset(kNavBarTitleLabelHeight);
    }];
    
    [backButton mas_remakeConstraints:^(MASConstraintMaker *make) {
        make.centerY.equalTo(self->containerView);
        make.left.offset(10);
    }];
    
    [rightButton mas_remakeConstraints:^(MASConstraintMaker *make) {
        make.centerY.equalTo(self->containerView);
        make.right.offset(-10);
        make.size.mas_equalTo(CGSizeMake(100, 40));
    }];
}

#pragma mark Event
- (void)onClickBackButton {
    [[UIViewController zw_currentViewController] zw_toLastViewController];
}

- (void)onClickRightButton {
    if ([self.delegate respondsToSelector:@selector(navBarDidSelectedRight)]) {
        [self.delegate navBarDidSelectedRight];
    }
}

#pragma mark Set
- (void)setType:(OTNavBarType)type {
    _type = type;
    switch (type) {
        case OTNavBarTypeDefault:
        {
            backButton.hidden = NO;
        }
            break;
        case OTNavBarTypeAdd:
        {
            backButton.hidden = NO;
            rightButton.hidden = NO;
        }
            break;
        default:
            break;
    }
}

- (void)setTitle:(NSString *)title
{
    _title = title;
    titleLabel.text = _title;
}

- (void)setRightTitle:(NSString *)rightTitle {
    _rightTitle = rightTitle;
    [rightButton setTitle:rightTitle forState:UIControlStateNormal];
}
@end
